package com.yunchao.library.socket.utils

import com.yunchao.library.socket.constants.Errors
import com.yunchao.library.socket.constants.Limits

object ValidationUtils {
    fun validateErrorLevel(level: Int) {
        require(level in listOf(0x00, 0x01, 0x02, 0xFF)) {
            "${Errors.INVALID_LEVEL}: 0x${level.toString(16)}"
        }
    }

    fun validateSession(session: Int) {
        require(session in 0..0xFF) { Errors.INVALID_SESSION }
    }

    fun validateIdHighBit(id: UInt) {
        require(id and 0x80000000u == 0u) { Errors.INVALID_ID_HIGH_BIT }
    }

    fun validateFragmentHighBit(fragment: UInt) {
        require(fragment and 0x80000000u == 0u) { Errors.INVALID_FRAGMENT_HIGH_BIT }
    }

    fun validateString(value: String, fieldName: String, maxLength: Int = Limits.MAX_STRING_LENGTH) {
        require(value.length <= maxLength) {
            "$fieldName length ${value.length} exceeds maximum $maxLength"
        }
    }
}